﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using ZFine.Data;
using ZFine.Domain.Entity;

namespace Logic.Repository.Project
{
    public class OperationGudaoCarRepository : RepositoryBase<OperationGudaoCarEntity>, IOperationGudaoCarRepository
    {
        public int Insert(OperationGudaoCarEntity entitys)
        {

            if (entitys.OperationPersion != null)
            {
                dbcontext.Entry<UserEntity>(entitys.OperationPersion).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.OperationPersion).CurrentValue = entitys.OperationPersion;
                //dbcontext.Set<CourseEntity>().Attach(entitys).CourseType = entitys.CourseType;
            }

            if (entitys.SalaryPeople != null)
            {
                dbcontext.Entry<UserEntity>(entitys.SalaryPeople).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.SalaryPeople).CurrentValue = entitys.SalaryPeople;
                //dbcontext.Set<CourseEntity>().Attach(entitys).CourseTime = entitys.CourseTime;
            }

            if (entitys.WorkPlan != null)
            {
                dbcontext.Entry<WorkPlanEntity>(entitys.WorkPlan).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.WorkPlan).CurrentValue = entitys.WorkPlan;
                //dbcontext.Set<CourseEntity>().Attach(entitys).PublicName = entitys.PublicName;
            }

            if (entitys.FaHuoYuan != null)
            {
                dbcontext.Entry<UserEntity>(entitys.FaHuoYuan).State = EntityState.Unchanged;
                dbcontext.Entry(entitys).Reference(f => f.FaHuoYuan).CurrentValue = entitys.FaHuoYuan;
            }
            dbcontext.Entry<OperationGudaoCarEntity>(entitys).State = EntityState.Added;
            return dbcontext.SaveChanges();
            //return dbcontext.SaveChanges();
        }

        public OperationGudaoCarEntity FindEntity(Expression<Func<OperationGudaoCarEntity, bool>> predicate)
        {
            return dbcontext.Set<OperationGudaoCarEntity>().Include(f => f.OperationPersion).Include(f => f.SalaryPeople)
                .Include(f => f.WorkPlan).FirstOrDefault(predicate);
        }

        public OperationGudaoCarEntity FindincludeTwo(Expression<Func<OperationGudaoCarEntity, bool>> predicate)
        {
            return dbcontext.Set<OperationGudaoCarEntity>().Include(f => f.OperationPersion).Include(f => f.SalaryPeople)
                .Include(f => f.WorkPlan).Include(f => f.WorkPlan.DiaoChePersion).Include(f => f.WorkPlan.DiaoDuPerson).FirstOrDefault(predicate);
        }

        /// <summary>
        /// 执行计划
        /// </summary>
        /// <param name="historygudaocarlist">保存后退操作用</param>
        /// <param name="result">删除项</param>
        /// <param name="newgudaolist">执行完成有效数据</param>
        /// <param name="thisoperationgudao">操作</param>
        /// <returns></returns>
        public int SubmitAllPlan(List<HistoryGudaoCarEntity> historygudaocarlist, List<GudaoCarEntity> result,
            List<GudaoCarEntity> newgudaolist, OperationGudaoCarEntity thisoperationgudao)
        {
            //dbcontext.Entry<OperationGudaoCarEntity>(thisoperationgudao).State = EntityState.Modified;
            for (int i = 0; i < historygudaocarlist.Count; i++)
            {
                historygudaocarlist[i].WorkPlanOper = thisoperationgudao;
                if (historygudaocarlist[i].WorkPlanOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(historygudaocarlist[i].WorkPlanOper).State = EntityState.Modified;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.WorkPlanOper).CurrentValue = historygudaocarlist[i].WorkPlanOper;
                }
                if (historygudaocarlist[i].BelongPathway != null)
                {
                    dbcontext.Entry<PathwayEntity>(historygudaocarlist[i].BelongPathway).State = EntityState.Unchanged;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.BelongPathway).CurrentValue = historygudaocarlist[i].BelongPathway;
                }
                if (historygudaocarlist[i].BelongCarriage != null)
                {
                    dbcontext.Entry<CarriageEntity>(historygudaocarlist[i].BelongCarriage).State = EntityState.Unchanged;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.BelongCarriage).CurrentValue = historygudaocarlist[i].BelongCarriage;
                }
                if (historygudaocarlist[i].Operation != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(historygudaocarlist[i].Operation).State = EntityState.Unchanged;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.Operation).CurrentValue = historygudaocarlist[i].Operation;
                }
                if (historygudaocarlist[i].PassOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(historygudaocarlist[i].PassOper).State = EntityState.Unchanged;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.PassOper).CurrentValue = historygudaocarlist[i].PassOper;
                }
                if (historygudaocarlist[i].QiPiaoOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(historygudaocarlist[i].QiPiaoOper).State = EntityState.Unchanged;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.QiPiaoOper).CurrentValue = historygudaocarlist[i].QiPiaoOper;
                }
                if (historygudaocarlist[i].DuiWeiOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(historygudaocarlist[i].DuiWeiOper).State = EntityState.Unchanged;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.DuiWeiOper).CurrentValue = historygudaocarlist[i].DuiWeiOper;
                }
                if (historygudaocarlist[i].RecoveryOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(historygudaocarlist[i].RecoveryOper).State = EntityState.Unchanged;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.RecoveryOper).CurrentValue = historygudaocarlist[i].RecoveryOper;
                }
                if (historygudaocarlist[i].ClearGudaoOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(historygudaocarlist[i].ClearGudaoOper).State = EntityState.Unchanged;
                    dbcontext.Entry(historygudaocarlist[i]).Reference(f => f.ClearGudaoOper).CurrentValue = historygudaocarlist[i].ClearGudaoOper;
                }
                dbcontext.Entry<HistoryGudaoCarEntity>(historygudaocarlist[i]).State = EntityState.Added;
            }

            for (int i = 0; i < result.Count; i++)
            {
                dbcontext.Entry<GudaoCarEntity>(result[i]).State = EntityState.Deleted;
            }
            for (int i = 0; i < newgudaolist.Count; i++)
            {
                newgudaolist[i].WorkPlanOper = thisoperationgudao;
                if (newgudaolist[i].WorkPlanOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].WorkPlanOper).State = EntityState.Modified;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.WorkPlanOper).CurrentValue = newgudaolist[i].WorkPlanOper;
                }
                if (newgudaolist[i].BelongPathway != null)
                {
                    dbcontext.Entry<PathwayEntity>(newgudaolist[i].BelongPathway).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.BelongPathway).CurrentValue = newgudaolist[i].BelongPathway;
                }
                if (newgudaolist[i].BelongCarriage != null)
                {
                    dbcontext.Entry<CarriageEntity>(newgudaolist[i].BelongCarriage).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.BelongCarriage).CurrentValue = newgudaolist[i].BelongCarriage;
                }
                if (newgudaolist[i].Operation != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].Operation).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.Operation).CurrentValue = newgudaolist[i].Operation;
                }
                if (newgudaolist[i].PassOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].PassOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.PassOper).CurrentValue = newgudaolist[i].PassOper;
                }
                if (newgudaolist[i].QiPiaoOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].QiPiaoOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.QiPiaoOper).CurrentValue = newgudaolist[i].QiPiaoOper;
                }
                if (newgudaolist[i].DuiWeiOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].DuiWeiOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.DuiWeiOper).CurrentValue = newgudaolist[i].DuiWeiOper;
                }
                if (newgudaolist[i].RecoveryOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].RecoveryOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.RecoveryOper).CurrentValue = newgudaolist[i].RecoveryOper;
                }
                if (newgudaolist[i].ClearGudaoOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].ClearGudaoOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.ClearGudaoOper).CurrentValue = newgudaolist[i].ClearGudaoOper;
                }
                dbcontext.Entry<GudaoCarEntity>(newgudaolist[i]).State = EntityState.Added;
            }
            return dbcontext.SaveChanges();
        }

        /// <summary>
        /// 回退计划
        /// </summary>
        /// <param name="retrunGudaocarlist">回退完成的有效信息</param>
        /// <param name="historylist">历史记录的要回退信息</param>
        /// <param name="thisoperationgudao">回退操作</param>
        /// <returns></returns>
        public int SubmitAllReturnPlan(List<GudaoCarEntity> newgudaolist, List<GudaoCarEntity> historylist,
            OperationGudaoCarEntity thisoperationgudao)
        {
            for (int i = 0; i < historylist.Count; i++)
            {
                dbcontext.Entry<GudaoCarEntity>(historylist[i]).State = EntityState.Deleted;
            }
            for (int i = 0; i < newgudaolist.Count; i++)
            {
                newgudaolist[i].WorkPlanOper = thisoperationgudao;
                if (newgudaolist[i].WorkPlanOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].WorkPlanOper).State = EntityState.Modified;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.WorkPlanOper).CurrentValue = newgudaolist[i].WorkPlanOper;
                }
                if (newgudaolist[i].BelongPathway != null)
                {
                    dbcontext.Entry<PathwayEntity>(newgudaolist[i].BelongPathway).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.BelongPathway).CurrentValue = newgudaolist[i].BelongPathway;
                }
                if (newgudaolist[i].BelongCarriage != null)
                {
                    dbcontext.Entry<CarriageEntity>(newgudaolist[i].BelongCarriage).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.BelongCarriage).CurrentValue = newgudaolist[i].BelongCarriage;
                }
                if (newgudaolist[i].Operation != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].Operation).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.Operation).CurrentValue = newgudaolist[i].Operation;
                }
                if (newgudaolist[i].PassOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].PassOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.PassOper).CurrentValue = newgudaolist[i].PassOper;
                }
                if (newgudaolist[i].QiPiaoOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].QiPiaoOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.QiPiaoOper).CurrentValue = newgudaolist[i].QiPiaoOper;
                }
                if (newgudaolist[i].ClearGudaoOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].ClearGudaoOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.ClearGudaoOper).CurrentValue = newgudaolist[i].ClearGudaoOper;
                }
                if (newgudaolist[i].DuiWeiOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].DuiWeiOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.DuiWeiOper).CurrentValue = newgudaolist[i].DuiWeiOper;
                }
                if (newgudaolist[i].RecoveryOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(newgudaolist[i].RecoveryOper).State = EntityState.Unchanged;
                    dbcontext.Entry(newgudaolist[i]).Reference(f => f.RecoveryOper).CurrentValue = newgudaolist[i].RecoveryOper;
                }
                dbcontext.Entry<GudaoCarEntity>(newgudaolist[i]).State = EntityState.Added;
            }
            return dbcontext.SaveChanges();
        }
    }
}
